<?php

declare(strict_types=1);
namespace App\Service;

use App\Constants\ErrorCode;
use App\Dao\RequestlogDao;
use App\Utils\ServerUtil;

class RequestlogService
{

    /**
     * 日志记录显示
     * @Author abell
     * @DateTime 2020-05-28
     * @param array $request
     * @return array
     */
    public function logList($request){
        try {
            $pageSize = array_key_exists('pageSize',$request) ? $request['pageSize'] : 15;
            $pageNum = array_key_exists('page',$request) ? (int)$request['page'] : 1;
            $where = [];
            $order = array_key_exists('order',$request) ? $request['order'] : 'add_time desc' ;
            if(array_key_exists('path',$request)) $where['path'] = $request['name'];
            if(array_key_exists('start_time',$request)){
                $end_time = array_key_exists('end_time',$request) ? $request['end_time'] : time();
                $result = make(RequestlogDao::class)->logsList($where,$order,$pageSize,$pageNum,'add_time',$request['start_time'],$end_time);
            }else{
                $result = make(RequestlogDao::class)->logsList($where,$order,$pageSize,$pageNum);
            }
            return ['code'=>ErrorCode::SUCCESS,'msg'=>ErrorCode::getMessage(ErrorCode::SUCCESS),'data'=>$result];
        }catch (\Exception $e) {
            ServerUtil::info('日志记录显示/admin/requestlog/log_list', ['error' => $e->getMessage(), 'request' => $request], 'ERROR');
            return ['code'=>ErrorCode::ERR_SERVER,'msg'=>ErrorCode::getMessage(ErrorCode::ERR_SERVER),'data'=>''];
        }
    }
}
